/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
// BO PHAN NAY VAN CHUYEN HANG
package com.sun.courier.view;

import com.sun.courier.view.support.DespatchWillReceive_ButtonsRenderer;
import com.sun.courier.view.support.DespatchWillReceive_ButtonsEditor;
import com.sun.courier.DemoProperties;
import com.sun.courier.dal.CourierDAL;
import com.sun.courier.dal.CourierManageDAL;
import com.sun.courier.dto.Courier_CourierManageDTO;
import com.sun.courier.utilities.Utilities;
import com.sun.courier.view.support.DespatchingSentStore_ButtonsEditor;
import com.sun.courier.view.support.DespatchingSentStore_ButtonsRenderer;
import com.sun.courier.view.support.DestinationStore_ButtonsEditor;
import com.sun.courier.view.support.DestinationStore_ButtonsRenderer;
import com.sun.courier.view.support.ReceivedStore_ButtonsEditor;
import com.sun.courier.view.support.ReceivedStore_ButtonsRenderer;
import com.sun.courier.view.support.SendingStore_ButtonsEditor;
import com.sun.courier.view.support.SendingStore_ButtonsRenderer;
import com.sun.courier.view.support.ToReturnCus_ButtonsEditor;
import com.sun.courier.view.support.ToReturnCus_ButtonsRenderer;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author HONG QUAN
 */
@DemoProperties(
        value = "Despatching",
        category = "Courier Management",
        description = "Despatch Couriers to Destinations",
        sourceFiles = { //                "com/sun/courier/view/EmployeeManagement.java",
        //                "com/sun/courier/view/resources/images/EmployeeManagement.png",
        }
)
public class CourierDespatching extends JPanel implements ActionListener {

    JPanel controlPanel;
    JComboBox cbSelectType = new JComboBox();
    JScrollPane scrollpane;

    JLabel search = new JLabel("Search:");
    JTextField txtSearch = new JTextField();
    private JButton btnSend = new JButton("Send");
    DefaultTableModel model = null;
    CourierManageDAL cmDal = new CourierManageDAL();
    CourierDAL courierDal = new CourierDAL();
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();

    public JTable despatchFromCol;
    String empId = Utilities.empId;
    String username = Utilities.username;

    private TableRowSorter<TableModel> rowSorter;
    
    public CourierDespatching() {
        ActionListeners();
        setLayout(new BorderLayout());
        controlPanel = createControlPanel();
        add(controlPanel, BorderLayout.NORTH);
        createWillReceiveCollJTable();
    }

    protected JPanel createControlPanel() {
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(gridbag);

        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 1;
        c.insets = new Insets(20, 10, 0, 10);
        c.anchor = GridBagConstraints.SOUTHWEST;
        controlPanel.add(search, c);

        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 1;
        c.insets.top = 0;
        c.insets.bottom = 0;
        c.insets.left = 10;
        c.anchor = GridBagConstraints.SOUTHWEST;
        txtSearch.setPreferredSize(new Dimension(300, 30));
        controlPanel.add(txtSearch, c);
        
        c.gridx = 0;
        c.gridy = 2;
        c.gridheight = 1;
        c.insets = new Insets(0, 10, 0, 10);
        c.anchor = GridBagConstraints.SOUTHWEST;
        JLabel courierSelection = new JLabel("Select A Type of Courier Management Method:");
        controlPanel.add(courierSelection, c);

        c.gridx = 0;
        c.gridy = 3;
        c.weightx = 1.0;
        c.insets.top = 0;
        c.insets.bottom = 12;
        c.anchor = GridBagConstraints.SOUTHWEST;
        cbSelectType.setPreferredSize(new Dimension(300, 30));
//        cbSelectType.addItem("Choose An Item");
        cbSelectType.addItem("Couriers Will Receive From Store");
        cbSelectType.addItem("Couriers Will Send To Destination Store");
        cbSelectType.addItem("Couriers Received From Store");
        cbSelectType.addItem("Couriers Sent Store");// OK
        cbSelectType.addItem("Couriers Sending Store");//CAI NAY CHUA TAO
        controlPanel.add(cbSelectType, c);

        c.gridx = 0;
        c.gridy = 3;
        c.weightx = 1.0;
        c.anchor = GridBagConstraints.EAST;
        controlPanel.add(btnSend, c);

        return controlPanel;
    }
///////////////////////////////0. Couriers Will Receive From Store////////////////////

    private void createWillReceiveCollJTable() {
        despatchFromCol = WillReceiveCollJTable();
//        table.setColumnModel(createColumnModel());
        rowSorter = new TableRowSorter<>(despatchFromCol.getModel());
        despatchFromCol.setRowSorter(rowSorter);
        despatchFromCol.setRowHeight(30);
        despatchFromCol.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        despatchFromCol.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        despatchFromCol.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = despatchFromCol.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * despatchFromCol.getRowHeight();
        despatchFromCol.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = despatchFromCol.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        scrollpane = new JScrollPane(despatchFromCol);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable WillReceiveCollJTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone",
            "Sender", "Status", "Update Time", "Actions", "SenderId"};
        Object[][] data = null;
        ArrayList<Courier_CourierManageDTO> lst = null;
        try {
            lst = cmDal.loadWillReceiveFromColleagues(Integer.parseInt(empId));//Login id o day.
            data = new Object[lst.size()][9];
            for (int i = 0; i < lst.size(); i++) {
                data[i][0] = lst.get(i).getC().getCourierId();
                data[i][1] = lst.get(i).getC().getCourierName();
                data[i][2] = lst.get(i).getC().getCustomerName();
                data[i][3] = lst.get(i).getC().getPhone();
                data[i][4] = courierDal.getEmployeeName(lst.get(i).getCm().getSender());
                data[i][5] = "Confirm Required";
                data[i][6] = lst.get(i).getCm().getUpdatetime();
                data[i][7] = empty;
                data[i][8] = lst.get(i).getCm().getSender();
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (lst.isEmpty()) {
            model = new DefaultTableModel(data, columnNames);
        } else {
            model = new DefaultTableModel(data, columnNames) {
                @Override
                public Class<?> getColumnClass(int column) {
                    return getValueAt(0, column).getClass();
                }
            };
        }

        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn columnActions = table.getColumnModel().getColumn(7);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setMaxWidth(0);
        column0.setMinWidth(0);
        column0.setWidth(0);
        column0.setPreferredWidth(0);
        TableColumn column8 = table.getColumnModel().getColumn(8);
        column8.setMaxWidth(0);
        column8.setMinWidth(0);
        column8.setWidth(0);
        column8.setPreferredWidth(0);

        columnActions.setCellRenderer(new DespatchWillReceive_ButtonsRenderer());
        columnActions.setCellEditor(new DespatchWillReceive_ButtonsEditor(table));
        return table;
    }
///////////////////////////////1. Couriers Will Send To Destination Store////////////////////

    private void createWillSendColJTable() {
        despatchFromCol = WillSendColJTable();
//        table.setColumnModel(createColumnModel());
        rowSorter = new TableRowSorter<>(despatchFromCol.getModel());
        despatchFromCol.setRowSorter(rowSorter);
        despatchFromCol.setRowHeight(30);
        despatchFromCol.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        despatchFromCol.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        despatchFromCol.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = despatchFromCol.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * despatchFromCol.getRowHeight();
        despatchFromCol.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = despatchFromCol.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        scrollpane = new JScrollPane(despatchFromCol);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable WillSendColJTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone",
            "Sender", "Status", "Update Time", "", "Actions", "SenderId"};
        Object[][] data = null;
        ArrayList<Courier_CourierManageDTO> lst = null;
        try {
            lst = cmDal.loadWillSendColleagues(Integer.parseInt(empId));//Login id o day.
            if (lst.isEmpty()) {
                data = new Object[0][10];
            } else {
                data = new Object[lst.size()][10];
                for (int i = 0; i < lst.size(); i++) {
                    data[i][0] = lst.get(i).getC().getCourierId();
                    data[i][1] = lst.get(i).getC().getCourierName();
                    data[i][2] = lst.get(i).getC().getCustomerName();
                    data[i][3] = lst.get(i).getC().getPhone();
                    data[i][4] = courierDal.getEmployeeName(lst.get(i).getCm().getSender());
                    data[i][5] = "Despatching";
                    data[i][6] = lst.get(i).getCm().getUpdatetime();
                    data[i][7] = new Boolean(false);
                    data[i][8] = empty;
                    data[i][9] = lst.get(i).getCm().getSender();
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (lst.isEmpty()) {
            model = new DefaultTableModel(data, columnNames);
        } else {
            if (model.getRowCount() < 1) {
                model = new DefaultTableModel(data, columnNames);
            } else {
                model = new DefaultTableModel(data, columnNames) {
                    @Override
                    public Class<?> getColumnClass(int column) {
                        return getValueAt(0, column).getClass();
                    }
                };
            }
        }

        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn columnActions = table.getColumnModel().getColumn(8);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setMaxWidth(0);
        column0.setMinWidth(0);
        column0.setWidth(0);
        column0.setPreferredWidth(0);
        TableColumn column7 = table.getColumnModel().getColumn(7);
        column7.setCellEditor(table.getDefaultEditor(Boolean.class));
        column7.setCellRenderer(table.getDefaultRenderer(Boolean.class));
        column7.setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));
        TableColumn column9 = table.getColumnModel().getColumn(9);
        column9.setMaxWidth(0);
        column9.setMinWidth(0);
        column9.setWidth(0);
        column9.setPreferredWidth(0);

        columnActions.setCellRenderer(new DestinationStore_ButtonsRenderer());
        columnActions.setCellEditor(new DestinationStore_ButtonsEditor(table));
        return table;
    }
///////////////////////////////2. COURIER RECEIVED FROM STORE/COLLEAGUES/////////////////////////

    private void createReceivedFromColJTable() {
        despatchFromCol = ReceivedFromColJTable();
//        table.setColumnModel(createColumnModel());
        rowSorter = new TableRowSorter<>(despatchFromCol.getModel());
        despatchFromCol.setRowSorter(rowSorter);
        despatchFromCol.setRowHeight(30);
        despatchFromCol.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        despatchFromCol.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        despatchFromCol.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = despatchFromCol.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * despatchFromCol.getRowHeight();
        despatchFromCol.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = despatchFromCol.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        scrollpane = new JScrollPane(despatchFromCol);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable ReceivedFromColJTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone",
            "Sender", "Status", "Update Time", "Actions", "SenderId"};
        Object[][] data = null;
        try {
            ArrayList<Courier_CourierManageDTO> lst = cmDal.loadReceivedFromColleagues(Integer.parseInt(empId));//Login id o day.
            data = new Object[lst.size()][9];
            for (int i = 0; i < lst.size(); i++) {
                data[i][0] = lst.get(i).getC().getCourierId();
                data[i][1] = lst.get(i).getC().getCourierName();
                data[i][2] = lst.get(i).getC().getCustomerName();
                data[i][3] = lst.get(i).getC().getPhone();
                data[i][4] = courierDal.getEmployeeName(lst.get(i).getCm().getSender());
                data[i][5] = "Received";
                data[i][6] = lst.get(i).getCm().getUpdatetime();
                data[i][7] = empty;
                data[i][8] = lst.get(i).getCm().getSender();
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        model = new DefaultTableModel(data, columnNames) {
            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };

        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn columnActions = table.getColumnModel().getColumn(7);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setMaxWidth(0);
        column0.setMinWidth(0);
        column0.setWidth(0);
        column0.setPreferredWidth(0);
        TableColumn column8 = table.getColumnModel().getColumn(8);
        column8.setMaxWidth(0);
        column8.setMinWidth(0);
        column8.setWidth(0);
        column8.setPreferredWidth(0);

        columnActions.setCellRenderer(new ReceivedStore_ButtonsRenderer());
        columnActions.setCellEditor(new ReceivedStore_ButtonsEditor(table));
        return table;
    }

    //////////////////////////////Couriers Sent Store/////////////////////////
    private void createSentColToJTable() {
        despatchFromCol = SentToColJTable();
//        table.setColumnModel(createColumnModel());
        rowSorter = new TableRowSorter<>(despatchFromCol.getModel());
        despatchFromCol.setRowSorter(rowSorter);
        despatchFromCol.setRowHeight(30);
        despatchFromCol.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        despatchFromCol.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        despatchFromCol.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = despatchFromCol.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * despatchFromCol.getRowHeight();
        despatchFromCol.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = despatchFromCol.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        scrollpane = new JScrollPane(despatchFromCol);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable SentToColJTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone",
            "Receiver", "Status", "Update Time", "Actions", "ReceiverId"};
        Object[][] data = null;
        ArrayList<Courier_CourierManageDTO> lst = null;
        try {
            lst = cmDal.loadSentToColleagues(Integer.parseInt(empId));//Login id o day.
            data = new Object[lst.size()][9];
            for (int i = 0; i < lst.size(); i++) {
                data[i][0] = lst.get(i).getC().getCourierId();
                data[i][1] = lst.get(i).getC().getCourierName();
                data[i][2] = lst.get(i).getC().getCustomerName();
                data[i][3] = lst.get(i).getC().getPhone();
                data[i][4] = courierDal.getEmployeeName(lst.get(i).getCm().getReceiver());
                data[i][5] = "Sent";
                data[i][6] = lst.get(i).getCm().getUpdatetime();
                data[i][7] = empty;
                data[i][8] = lst.get(i).getCm().getReceiver();
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (lst.isEmpty()) {
            model = new DefaultTableModel(data, columnNames);
        } else {
            model = new DefaultTableModel(data, columnNames) {
                @Override
                public Class<?> getColumnClass(int column) {
                    return getValueAt(0, column).getClass();
                }
            };
        }

        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn columnActions = table.getColumnModel().getColumn(7);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setMaxWidth(0);
        column0.setMinWidth(0);
        column0.setWidth(0);
        column0.setPreferredWidth(0);
        TableColumn column8 = table.getColumnModel().getColumn(8);
        column8.setMaxWidth(0);
        column8.setMinWidth(0);
        column8.setWidth(0);
        column8.setPreferredWidth(0);

        columnActions.setCellRenderer(new DespatchingSentStore_ButtonsRenderer());
        columnActions.setCellEditor(new DespatchingSentStore_ButtonsEditor(table));
        return table;
    }
/////////////////////////////////////////////////////////COURIER SENDING STORE//////////////////////////////

    private void createCourierSedingStoreJTable() {
        despatchFromCol = CourierSedingStoreJTable();
//        table.setColumnModel(createColumnModel());
        rowSorter = new TableRowSorter<>(despatchFromCol.getModel());
        despatchFromCol.setRowSorter(rowSorter);
        despatchFromCol.setRowHeight(30);
        despatchFromCol.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        despatchFromCol.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        despatchFromCol.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = despatchFromCol.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * despatchFromCol.getRowHeight();
        despatchFromCol.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = despatchFromCol.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        scrollpane = new JScrollPane(despatchFromCol);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable CourierSedingStoreJTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone",
            "Receiver", "Status", "Update Time", "Actions", "ReceiverId"};
        Object[][] data = null;
        ArrayList<Courier_CourierManageDTO> lst = null;
        try {
            lst = cmDal.loadCourierSedingStoreJTable(Integer.parseInt(empId));//Login id o day.
            data = new Object[lst.size()][9];
            for (int i = 0; i < lst.size(); i++) {
                data[i][0] = lst.get(i).getC().getCourierId();
                data[i][1] = lst.get(i).getC().getCourierName();
                data[i][2] = lst.get(i).getC().getCustomerName();
                data[i][3] = lst.get(i).getC().getPhone();
                data[i][4] = courierDal.getEmployeeName(lst.get(i).getCm().getReceiver());
                data[i][5] = "Sending";
                data[i][6] = lst.get(i).getCm().getUpdatetime();
                data[i][7] = empty;
                data[i][8] = lst.get(i).getCm().getReceiver();
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (lst.isEmpty()) {
            model = new DefaultTableModel(data, columnNames);
        } else {
            model = new DefaultTableModel(data, columnNames) {
                @Override
                public Class<?> getColumnClass(int column) {
                    return getValueAt(0, column).getClass();
                }
            };
        }

        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn columnActions = table.getColumnModel().getColumn(7);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setMaxWidth(0);
        column0.setMinWidth(0);
        column0.setWidth(0);
        column0.setPreferredWidth(0);
        TableColumn column8 = table.getColumnModel().getColumn(8);
        column8.setMaxWidth(0);
        column8.setMinWidth(0);
        column8.setWidth(0);
        column8.setPreferredWidth(0);

        columnActions.setCellRenderer(new SendingStore_ButtonsRenderer());
        columnActions.setCellEditor(new SendingStore_ButtonsEditor(table));
        return table;
    }
////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public void ActionListeners() {
        cbSelectType.addActionListener(this);
        btnSend.addActionListener(this);
        searchFilter();
    }

    public void searchFilter() {
        txtSearch.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = txtSearch.getText();

                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = txtSearch.getText();

                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

        });
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(cbSelectType)) {
            if (cbSelectType.getSelectedIndex() == 0) {
                if (scrollpane == null) {
                    btnSend.setVisible(false);
                    createWillReceiveCollJTable();//OK
                } else {
                    btnSend.setVisible(false);
                    remove(scrollpane);
                    createWillReceiveCollJTable();
                    invalidate();
                    validate();
                    repaint();
                }
            } else if (cbSelectType.getSelectedIndex() == 1) {
                btnSend.setVisible(true);
                remove(scrollpane);
                createWillSendColJTable();//OK
                invalidate();
                validate();
                repaint();
            } else if (cbSelectType.getSelectedIndex() == 2) {
                btnSend.setVisible(false);
                remove(scrollpane);
                createReceivedFromColJTable();//OK
                invalidate();
                validate();
                repaint();
            } else if (cbSelectType.getSelectedIndex() == 3) {
                btnSend.setVisible(false);
                remove(scrollpane);
                createSentColToJTable();
                invalidate();
                validate();
                repaint();
            } else if (cbSelectType.getSelectedIndex() == 4) {
                btnSend.setVisible(false);
                remove(scrollpane);
                createCourierSedingStoreJTable();
                invalidate();
                validate();
                repaint();
            }
        }
        if (e.getSource().equals(btnSend)) {
            ArrayList lst = new ArrayList();
            int kq = 0;
            for (int i = 0; i < despatchFromCol.getRowCount(); i++) {
                if (despatchFromCol.getValueAt(i, 7).equals(true) && despatchFromCol.getValueAt(i, 5).equals("Despatching")) {
                    kq = 1;
                    lst.add(despatchFromCol.getValueAt(i, 0));
                }
            }
            if (kq == 0) {
                JOptionPane.showMessageDialog(controlPanel, "You do not choose courier or courier was sent!", "Result", JOptionPane.INFORMATION_MESSAGE);
                return;
            } else {
                SendToDestinationStoreActions s = new SendToDestinationStoreActions(model, lst);
                s.setAlwaysOnTop(true);
                s.setModal(true);
                s.setSize(new Dimension(600, 400));
                s.setVisible(true);
            }
        }
    }

    //INNER CHECK BOX CLASS
    class MyItemListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {
            Object source = e.getSource();
            if (source instanceof AbstractButton == false) {
                return;
            }
            boolean checked = e.getStateChange() == ItemEvent.SELECTED;
            for (int x = 0, y = despatchFromCol.getRowCount(); x < y; x++) {
                despatchFromCol.setValueAt(new Boolean(checked), x, 7);
            }
        }
    }

    class CheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {

        protected CheckBoxHeader rendererComponent;
        protected int column;
        protected boolean mousePressed = false;

        public CheckBoxHeader(ItemListener itemListener) {
            rendererComponent = this;
            rendererComponent.addItemListener(itemListener);
        }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

            if (table != null) {
                JTableHeader header = table.getTableHeader();
                if (header != null) {
                    rendererComponent.setForeground(header.getForeground());
                    rendererComponent.setBackground(header.getBackground());
                    rendererComponent.setFont(header.getFont());
                    header.addMouseListener(rendererComponent);
                }
            }
            rendererComponent.setText("Check All");
            setColumn(column);

            setBorder(UIManager.getBorder("TableHeader.cellBorder"));
            return rendererComponent;
        }

        protected void setColumn(int column) {
            this.column = column;
        }

        public int getColumn() {
            return column;
        }

        protected void handleClickEvent(MouseEvent e) {
            if (mousePressed) {
                mousePressed = false;
                JTableHeader header = (JTableHeader) (e.getSource());
                JTable courierToReturnTable = header.getTable();
                TableColumnModel columnModel = courierToReturnTable.getColumnModel();
                int viewColumn = columnModel.getColumnIndexAtX(e.getX());
                int column = courierToReturnTable.convertColumnIndexToModel(viewColumn);

                if (viewColumn == this.column && e.getClickCount() == 1 && column != -1) {
                    doClick();
                }
            }
        }

        public void mouseClicked(MouseEvent e) {
            handleClickEvent(e);
            ((JTableHeader) e.getSource()).repaint();
        }

        public void mousePressed(MouseEvent e) {
            mousePressed = true;
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    }

    public static void main(String[] args) throws Exception {
        JFrame f = new JFrame();
        CourierDespatching c = new CourierDespatching();
        f.add(c);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(new Dimension(700, 500));
        f.setVisible(true);
    }
}
